package com.retailers.generator.service;

import com.retailers.generator.bean.ColumnBean;
import com.retailers.generator.bean.Constant;
import com.retailers.generator.bean.TableBean;
import org.apache.commons.lang3.text.WordUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: honglei
 * @time: 2021/3/23 0023 17:16
 */
public interface MybatisGenService {

    /*获取表集合*/
    List<TableBean> tablesList() throws Exception;

    List<ColumnBean> getColumns(String table) throws Exception;

    /*指定表代码生成*/
    default boolean genCode(List<TableBean> tableNames) throws Exception{
        for (TableBean tableName : tableNames) {
            genCode(tableName);
        }
        return true;
    }

    /*指定表代码生成*/
    boolean genCode(TableBean columnBean) throws Exception;









    default String tableToJava(String tableName, String tablePrefix){
        return tableToJava(tableName,tablePrefix,true);
    }

    /**
     * 表名转java 类名
     * @param tableName 表名
     * @param tablePrefix 前缀
     * @param autoRemovePre 是否去除前缀
     * @return
     */
    default String tableToJava(String tableName, String tablePrefix, boolean autoRemovePre){
        if (autoRemovePre) {
            tableName = tableName.substring(tableName.indexOf("_") + 1);
        }
        if (StringUtils.hasLength(tablePrefix)) {
            tableName = tableName.replace(tablePrefix, "");
        }

        return columnToJava(tableName);
    }

    default String columnToJava(String columnName) {
        return WordUtils.capitalizeFully(columnName, new char[]{'_'}).replace("_", "");
    }




    default String replaceUnderLine2UpperCase(String str){
        if (!StringUtils.hasLength(str)){
            return null;
        }
        StringBuffer sb = new StringBuffer();
        sb.append(str);
        int count = sb.indexOf("_");
        if (!str.contains("_")){
            return str;
        }
        while(count!=0){
            int num = sb.indexOf("_",count);
            count = num + 1;
            if(num != -1){
                char ss = sb.charAt(count);
                char ia = (char) (ss - 32);
                sb.replace(count , count + 1,ia + "");
            }
        }
        String result = sb.toString().replaceAll("_","");
        return result;
    }
}
